<?php

    class ContractCenterController extends Controller
    {

        public function actionIndex()
        {
            $this->render('index');
        }

        public function actionRenewalCenter($type)
        {
            $typeModel = ContractType::model()->find('description=:description', array(
                'description' => $type
                    ));
            if ($typeModel != null)
            {
                $this->render('renewalCenter', array(
                    'type' => $typeModel->id
                ));
            }
        }

        public function actionTypeStaticData()
        {
            echo $this->renderPartial('_typeStatic', array(
                    ), true, false);
        }
        
        public function actionListAll($query=NULL, $custno=NULL, $page=0, $sort=NULL, $status=null,$type=null,$billingstatus=null)
        {
            $crit = new CDbCriteria();
            if ($sort != NULL)
            {
                $order = CJSON::decode($sort);
                $crit->order = $order[0]['property'] . ' ' . $order[0]['direction'];
            }
            else
            {
                $crit->order = 'CAST( SUBSTR( c.contractid, 1,LOCATE(  "-", c.contractid )-1  ) AS SIGNED) ,CAST( SUBSTR( c.contractid, LOCATE(  "-", c.contractid ) +1 ) AS SIGNED )';
            }
            $crit->alias = 'c';
            $join = '';
            if ($query != NULL)
            {

                $join = 'left join tbl_contract_type ct on ct.id= c.contractype
                         left join tbl_contract_status cs on cs.id= c.status
                         left join tbl_locations l on l.locno = c.locno and l.custno=c.custno
                         
                ';
                $crit->condition = 'l.locname=:query
                or l.state=:query or l.zip=:query or l.state=:query
                or ct.description like :like or cs.description like :like
                or c.contractstartdate =:query or c.contractid=:query
                or l.add1 like :like
                ';
                $crit->params['query'] = $query;
                $crit->params['like'] = "%$query%";
            }
            if ($custno != NULL)
            {
                $crit->addCondition('c.custno=:custno');
                $crit->params['custno'] = $custno;
            }
            $crit->compare('c.status', $status);            
            $crit->compare('c.contractype', $type);
            $crit->compare('c.billingstatus', $billingstatus);
            $join .=' left join tbl_contracts c2 on c2.orginalcontractid = c.contractid';
            $crit->join = $join;
            
            $crit->addCondition('datediff(now(),c.contractenddate) >= - 45 and c2.contractid is null');
            $pagination = new CPagination;
            if ($page > 0)
                $page--;
            if ($page >= 0)
            {
                $pagination->setCurrentPage($page);
                $pagination->setPageSize(25);
            }
            else
            {
                $pagination->setCurrentPage($page);
                $pagination->setPageSize(999999999); // ìninite!!!!!!!
            }
            $list = new CActiveDataProvider('Contracts', array(
                        'criteria' => $crit,
                        'pagination' => $pagination
                    ));
            $rs = array();
            if ($list != NULL && $list->getTotalItemCount() > 0)
            {

                $rs['totalCount'] = $list->getTotalItemCount();
                foreach ($list->getData() as $i => $item)
                {
                    $rs['data'][$i] = $item->attributes;
                    $rs['data'][$i]['statusText'] = $item->getStatusText();
                    $rs['data'][$i]['contracttypeText'] = ContractType::getText($item->contractype);
                    $location = $item->location;
                    if ($location != NULL)
                    {
                        $rs['data'][$i]['add1'] = $location->add1;
                        $rs['data'][$i]['locname'] = $location->locname;
                        $rs['data'][$i]['csz'] = $location->city . ' ' . $location->state . ' ' . $location->zip;
                    }
                    else
                    {
                        $rs['data'][$i]['locname'] = '';
                        $rs['data'][$i]['csz'] = '';
                    }
                    $invSum = 0;
                    $invPaidSum = 0;
                    foreach ($item->invoiceAndCMList as $invoice)
                    {
                        $invSum+= $invoice->getAmountWithDiscount();
                        $invPaidSum +=$invoice->getPaymentApplied();
                    }
                    $rs['data'][$i]['billedAmount'] = $invSum;
                    $rs['data'][$i]['paid'] = $invPaidSum;
                }
            }
            echo json_encode($rs);
        }

    }

?>
